import User from '../model/user.model.js'

class UserService {
  // 注册用户
  async createUser(user_name, password) {
    const res = await User.create({ user_name, password })
    return res.dataValues
  }

  // 获取用户信息
  async getUserInfo({ id, user_name, password }) {
    const whereOpt = {}
    id && Object.assign(whereOpt, { id })
    user_name && Object.assign(whereOpt, { user_name })
    password && Object.assign(whereOpt, { password })
    const res = await User.findOne({
      where: whereOpt,
      attributes: ['id', 'user_name', 'password']
    })
    return res ? res.dataValues : null
  }

  // 修改密码
  async updateById({ id, user_name, password }) {
    const whereOpt = { id }
    const newUser = {}
    user_name && Object.assign(newUser, { user_name })
    password && Object.assign(newUser, { password })
    const res = await User.update(newUser, { where: whereOpt })
    return res[0] > 0
  }

  // 保存用户资料
  async saveUserData({ user_name, password }) {
    return 0
  }
}

export default new UserService()